package org.wisdom.ac.apis.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.wisdom.ac.apis.RoleController;
import org.wisdom.ac.apis.assembler.RoleAssembler;
import org.wisdom.ac.domain.role.entity.AcRole;
import org.wisdom.ac.domain.role.repository.RoleRepository;
import org.wisdom.ac.domain.user.repository.UserRepository;
import org.wisdom.ac.dto.RoleDto;
import org.wisdom.annotation.WisdomController;
import org.wisdom.exception.BusinessException;
import org.wisdom.vo.PageModel;
import org.wisdom.vo.ResponseResult;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 系统角色API
 *
 * @author 张智研
 * @date 2022/05/31 15:13
 **/
@Slf4j
@RequiredArgsConstructor
@WisdomController
public class RoleControllerImpl implements RoleController {

    private final RoleRepository roleRepository;
    private final RoleAssembler roleAssembler;
    private final UserRepository userRepository;

    @Override
    public ResponseResult<String> save(RoleDto roleDto) {
        AcRole role = this.roleAssembler.roleDtoToEntity(roleDto);
        role = roleRepository.save(role);
        return ResponseResult.ok(role.getId());
    }

    @Override
    public ResponseResult<RoleDto> get(String roleId) {
        AcRole role = this.roleRepository.findById(roleId).orElse(null);
        if (role == null) {
            throw new BusinessException("角色" + roleId + "不存在！");
        }
        RoleDto roleDto = this.roleAssembler.roleEntityToDto(role);
        return ResponseResult.ok(roleDto);
    }

    @Override
    public ResponseResult<String> delete(String roleIds) {
        List<String> ids = Arrays.stream(roleIds.split(",")).collect(Collectors.toList());
        for(String id : ids) {
            Optional<AcRole> optionalAcRole = this.roleRepository.findById(id);
            optionalAcRole.ifPresent(this.roleRepository::delete);
        }
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult<String> batchDelete(List<String> roleIds) {
        roleIds.forEach(this.roleRepository::deleteById);
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult<PageModel<RoleDto>> list(RoleDto roleDto) {
        Sort sort = Sort.by(Sort.Direction.ASC, "code");
        AcRole role = this.roleAssembler.roleDtoToEntity(roleDto);
        Page<AcRole> rolePage =
                this.roleRepository.findAll(role, roleDto.getPageNumber() - 1, roleDto.getPageSize(), sort);
        PageModel<RoleDto> pageModel = new PageModel<>();
        List<RoleDto> roleDtoList = this.roleAssembler.roleEntityToDto(rolePage.getContent());
        pageModel.setContent(roleDtoList);
        pageModel.setTotalPages(rolePage.getTotalPages());
        pageModel.setTotalElements(rolePage.getTotalElements());
        pageModel.setPageNumber(rolePage.getNumber() + 1);
        pageModel.setPageSize(rolePage.getSize());
        return ResponseResult.ok(pageModel);
    }

}
